package com.vstudy.zx.dao.provider;

import com.vstudy.zx.entity.Article;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Set;

public class ArticleDaoProvider {
    public String insert(Article article) {
        String sql = new SQL() {{
            INSERT_INTO("tb_article");
            if (article.getArticle_title() != null) {
                VALUES("article_title", "#{article_title}");
            }
            if (article.getArticle_content() != null) {
                VALUES("article_content", "#{article_content}");
            }
            if (article.getOutline() != null) {
                VALUES("outline_id", "#{outline.o_id}");
            }
            if (article.getArticle_release_time() != null) {
                VALUES("article_release_time", "#{article_release_time}");
            }
            if (article.getAuthor() != null) {
                VALUES("article_author", "#{author.te_id}");
            }
        }}.toString();
        return sql;
    }

    public String selectByIds(@Param("ids") Set<Long> ids) {
        String sql = new SQL() {{
            SELECT("*");
            FROM("tb_article");
        }}.toString();
        Object[] arr_ids = ids.toArray();
        if (arr_ids.length > 0) {
            sql += " where article_id in (";
        }
        for (Long id :
                ids) {
            sql += id + ",";
        }
        sql = sql.substring(0, sql.length() - 1);
        sql += ")";
        return sql;
    }

//    public String delete(Long id){
//        String sql = new SQL(){{
//        }}.toString();
//        return "";
//    }
}
